<template>
	<view v-if="show">
		<Header title="授权"></Header>
		
		<view class="top">
			<view class="ava">
				<open-data style="width: 100%; height: 100%;" type="userAvatarUrl"></open-data>
			</view>
			
			<view class="name">
				<open-data type="userNickName" lang="zh_CN"></open-data>
			</view>
		</view>
		
		
		
		<view  class="cardNum">
			<view class="txt" style="color: #333333;font-size: 34upx;">{{info.organizationName||''}}</view>
			<view class="txt" v-if="gradeid"  style="font-size: 32upx;color: #909399;">{{info.gradeName||''}}</view>
			<view class="pick" v-else>
				<picker style="width: 100%; height: 100%;" mode="selector" range-key="organizationName" :range="list" @change="change($event)">
					<view style="width: 100%;height: 100%;line-height: 60upx;">{{className}}</view>
				</picker>
			</view>
			<input v-model="childName" type="text" value="" placeholder="请输入学生姓名或者别名" placeholder-style="font-size: 12px" />
		</view>
		
		
		<button  style="background-color:rgb(7,193,96) ;" type="primary" class="button" open-type='getPhoneNumber' @getphonenumber="getPhoneNumber">授权</button>
	
		<navigator class="button" style="background-color:#BBBBBB;" target="miniProgram" open-type="exit">取消</navigator>
<!-- 		<view  class="text" style="color: #333333;font-size: 28upx;bottom: 150upx;">申请获取您的手机号码</view>
		<view  class="text" style="font-size: 24upx;color: #909399;bottom: 100upx;">授权后通过手机号码可绑定学生信息</view> -->
		<view  class="text" style="font-size: 24upx;color: #909399;bottom: 50upx;">点击授权表示您已同意<text @click="toagreement" style="color: #007AFF;">《用户服务协议》</text></view>
	</view>
</template>

<script>
	var WXBizDataCrypt = require('@/utils/RdWXBizDataCrypt.js');
	export default {
		data() {
			return {
				appId:getApp().globalData.appId,
				sessionkey:uni.getStorageSync('sk'),
				wxOpenId:uni.getStorageSync('openId'),
				pubopenid:'',
				gradeid:'',
				schoolId:'',
				info:'',
				newUser:false,
				childName:'',
				show:false,
				className:'请选择班级',
				list:[]
			}
		},
		methods: {
			login(){
				let that=this
				
				uni.login({
					success(res2) {
						that.getOpenId(res2.code)
					}
				})
			},
			getOpenId(code){
				this.$http('/pay/api/wechart/getSessionNew',{
					code:code,
					appId:this.appId
				},res3=>{
					
					this.wxOpenId = res3.data.openid
					this.sessionkey = res3.data.sk?res3.data.sk : res3.data.session_key
					
					uni.setStorageSync('openId',res3.data.openid)
					uni.setStorageSync('sk',res3.data.sk)
					
					if(this.gradeid){
						this.getOrganizationId()
					}else{
						this.getClassList()
					}
				},'GET')
			},
			getClassList(){
				this.$http('/member/api/organization/list',{
					"condition":{"parentId":this.schoolId}
				},res=>{
					this.list = res.data.items
					this.getOrganizationId()
				})
			},
			getOrganizationId(){
				this.$http('/minprogram/api/member/GetOrganizationInfoByGrade',{
					"schoolId":this.schoolId,
					"gradeId":this.gradeid,
					"openId":this.wxOpenId
				},res=>{
					this.info = res.data
					this.getLogin()
				},'GET')
			},
			getLogin(){
				this.$http('/minprogram/api/member/verify',{
					"wxOpenId":uni.getStorageSync('openId'),
					"pubopenid":this.pubopenid
				},res4=>{	
						uni.setStorageSync('token',res4.data.token)
						if(this.info.faceCount>0){
							uni.reLaunch({
								url:'/pages/index/index?type=2'
							})
						}else{
							this.show = true
						}
				},'POST',()=>{
					uni.hideToast()
					this.newUser = true
					this.show = true
				})
			},
			async getPhoneNumber(e) {
				let that = this
				if(e.detail.errMsg=="getPhoneNumber:fail user deny"){       //用户决绝授权  
				}else{
					
					var pc = new WXBizDataCrypt(this.appId, this.sessionkey);
					var data = pc.decryptData(e.detail.encryptedData, e.detail.iv)
					if(this.newUser){
						
						await new Promise((aaa,bbb)=>{
							this.$http('/minprogram/api/member/bind',{
								"wxOpenId":this.wxOpenId,
								"phone":data.phoneNumber,
								"wxName":data.phoneNumber,
								"pubOpenId": this.pubopenid
							},result=>{
								uni.setStorageSync('token',result.data.token)
								aaa()
							})
						})
						this.newUser = false
					}
					
					
					this.$http('/minprogram/api/member/getStudentInfo',{
						"ownerName": this.childName.trim(),
						"schoolId": this.schoolId,
						"gradeId": this.gradeid?this.gradeid:this.info.gradeId,
					},res=>{
						if(res.data.phone){
							if(res.data.phone == data.phoneNumber){
								
								uni.reLaunch({
									url:'/pages/index/index?type=2'
								})
								
							}else{
								if(res.data.isSign){
									
									uni.showToast({
										title:'该学生已签约',
										icon:'none'
									})
								}else{
									uni.showModal({
									    content: '已绑定的号码为初始号码，请修改为家长自己的手机号码。',
										cancelText:'我知道了',
										confirmText:'立即修改',
									    success: function (suc) {
									        if (suc.confirm) {
												uni.navigateTo({
													url:`/pages/changePhone/changePhone?oldPhone=${res.data.phone}&newPhone=${data.phoneNumber}&ownerName=${that.childName.trim()}&gradeId=${that.gradeid?that.gradeid:that.info.gradeId}&organizationId=${that.schoolId}&id=${res.data.id}&schoolName=${that.info.organizationName}&className=${that.gradeid?that.info.gradeName:that.className}`
												})
												
									        } else if (suc.cancel) {
									        }
									    }
									});
								}
								
							}
						}else{
							this.addFace()
							}
					},'GET')
					
					
					
				}
			},
			// d53bfe79-8072-40cc-42a8-08d91b2fceaf
			change(e){
				this.className = this.list[e.detail.value].organizationName
				this.info.gradeId = this.list[e.detail.value].id
			},
			addFace(){
				this.$http('/minprogram/api/member/saveOwnerStudent',{
					"ownerName": this.childName.trim(),
					"organizationId": this.schoolId,
					"gradeId": this.gradeid?this.gradeid:this.info.gradeId,
				},res=>{
					if(res.data.value){
						uni.reLaunch({
							url:'/pages/index/index?type=2'
						})
					}
				})
			},
			toagreement(){
				uni.navigateTo({
					url:'/pages/agreement/agreement'
				})
			}
		},
		onLoad(e) {
			if(e.pubopenid){
				this.pubopenid = e.pubopenid
				this.schoolId = e.schoolId
				this.gradeid = e.gradeid
				this.login()
			}
		}
	}
</script>

<style scoped lang="less">
	.ava{
		width: 150upx;
		height: 150upx;
		border-radius: 50%;
		overflow: hidden;
		margin: 0 auto 0;
	}
	.name{
		width: 100%;
		text-align: center;
		margin-top: 2vh;
	}
	.top{
		width: 93%;
		margin: 0 auto;
		background-color: #FFFFFF;
		border-radius: 10upx;
		padding: 100upx 0 50upx;
		margin-top: 20upx;
	}
	.cardNum{
		width: 93%;
		margin: 1vh auto 8vh;
		background-color: #FFFFFF;
		border-radius: 10upx;
		padding-bottom: 40upx;
		>view.txt{
			height: 80upx;
			font-size: 30upx;
			line-height: 80upx;
			width: 100%;
			text-align: center;
		}
		>view.pick{
			width: 60%;
			height: 60upx;
			border: 1upx solid #cccccc;
			box-sizing: border-box;
			padding: 0 20upx;
			margin: 20upx auto;
			text-align: center;
		}
		>input{
			border: 1upx solid #cccccc;
			margin: 0 auto;
			text-align: center;
			background-color: #FFFFFF;
			width: 60%;
			height: 80upx;
			line-height: 80upx;
			box-sizing: border-box;
			padding: 0 30upx;
			border-radius: 10upx;
		}
	}
	.button{
		width: 90%;
		height: 96upx;
		line-height: 96upx;
		text-align: center;
		color: #FFFFFF;
		font-size: 30upx;
		margin: 30upx auto;
		border-radius: 10upx;
	}
	.text{
		width: 100%;
		text-align: center;
		color: #aaaaaa;
		font-size: 24upx;
		position: absolute;
	}
</style>
